import {RouteConfig,} from "vue-router";
import {Vue} from "vue-property-decorator";


export interface IPage {
    name: string
    title: string
    icon?: string
    component: object
    child?: Array<IPage>
}

export interface PageOpts {
    path:string
    name: string
    title: string
    icon?: string
    component: object
    child?: Array<PageOpts>
}

function expandOpts(pages: Array<IPage>): Array<PageOpts> {

    return pages.map<PageOpts>(
        (page) => {

            let path: string = '/' + page.name;

            return {
                path:'/' + page.name,
                name:page.name,
                title:page.title,
                icon:page.icon,
                component:page.component,
                child: page.child?expandOpts(page.child):undefined
            };
        }
    )
}

export class Pages {
    constructor(public items: Array<IPage>) {
    }

    Expand() :Array<RouteConfig> {
        return expand(this.GetPageOpts())
    }

    GetPageOpts(): Array<PageOpts> {
        return expandOpts(this.items)
    }
}

function expand(pages: Array<PageOpts>): Array<RouteConfig> {


    let opts: Array<RouteConfig> = [];

    pages.forEach((page) => {

        opts.push({
            path: page.path,
            name: page.name,
            component: page.component,
            children: page.child ? expand(page.child) : undefined
        })
    });

    return opts
}
